<?php
/******************************
 * EQdkp
 * Copyright 2002-2003
 * Licensed under the GNU GPL.  See COPYING for full terms.
 * ------------------
 * viewraid.php
 * Began: Thu December 19 2002
 * 
 * $Id: viewraid.php 541 2008-05-20 06:56:16Z rspeicher $
 * 
 ******************************/
 
define('EQDKP_INC', true);
$eqdkp_root_path = './';
include_once($eqdkp_root_path . 'common.php');

// Start session management
$user->start();
$user->setup();
$user->check_auth('u_raid_view');

if ( (isset($_GET[URI_RAID])) && (intval($_GET[URI_RAID] > 0)) )
{
    $sql = 'SELECT raid_id, raid_name, raid_date, raid_note, raid_value, raid_added_by, raid_updated_by
            FROM ' . RAIDS_TABLE . "
            WHERE raid_id='".$_GET[URI_RAID]."'";
            
    if ( !($raid_result = $db->query($sql)) )
    {
        message_die('Could not obtain raid information', '', __FILE__, __LINE__, $sql);
    }
    
    // Check for valid raid
    if ( !$raid = $db->fetch_record($raid_result) )
    {
        message_die($user->lang['error_invalid_raid_provided']);
    }
    $db->free_result($raid_result);
    
    //
    // Attendees
    //
    $sql = 'SELECT member_name
            FROM ' . RAID_ATTENDEES_TABLE . "
            WHERE raid_id='".$raid['raid_id']."'
            ORDER BY member_name";
    $result = $db->query($sql);    
    while ( $arow = $db->fetch_record($result) )
    {
        $attendees[] = $arow['member_name'];
    }
    $db->free_result($result);
    
    //$attendees = explode(',', $raid['raid_attendees']);
    
    if ( @sizeof($attendees) > 0 )
    {
        // First get rid of duplicates and resort them just in case,
        // so we're sure they're displayed correctly
        $attendees = array_unique($attendees);
        sort($attendees);
        reset($attendees);
        $rows = ceil(sizeof($attendees) / $user->style['attendees_columns']);
        
        // First loop: iterate through the rows
        // Second loop: iterate through the columns as defined in template_config,
        // then "add" an array to $block_vars that contains the column definitions,
        // then assign the block vars.
        // Prevents one column from being assigned and the rest of the columns for 
        // that row being blank
        for ( $i = 0; $i < $rows; $i++ )
        {
            $block_vars = array();
            for ( $j = 0; $j < $user->style['attendees_columns']; $j++ )
            {
                $offset = ($i + ($rows * $j));
                $block_vars += array(
                    'COLUMN'.$j.'_NAME' => ( isset($attendees[$offset]) ) ? '<a href="viewmember.php'.$SID.'&amp;' . URI_NAME . '='.$attendees[$offset].'">'.$attendees[$offset].'</a>' : ''
                );
                
                // Are we showing this column?
                $s_column = 's_column'.$j;
                ${$s_column} = true;
            }
            $tpl->assign_block_vars('attendees_row', $block_vars);
        }
        $column_width = floor(100 / $user->style['attendees_columns']);
    }
    else
    {
        message_die('Could not get raid attendee information.','Critical Error');
    }
    
    //
    // Drops
    //
    $sql = 'SELECT item_id, item_buyer, item_name, item_value
            FROM ' . ITEMS_TABLE . "
            WHERE raid_id='".$raid['raid_id']."'";
    if ( !($items_result = $db->query($sql)) )
    {
        message_die('Could not obtain item information', '', __FILE__, __LINE__, $sql);
    }
    while ( $item = $db->fetch_record($items_result) )
    {
        $tpl->assign_block_vars('items_row', array(
            'ROW_CLASS' => $eqdkp->switch_row_class(),
            'BUYER' => $item['item_buyer'],
            'U_VIEW_BUYER' => 'viewmember.php'.$SID.'&amp;' . URI_NAME . '='.$item['item_buyer'],
            'NAME' => stripslashes($item['item_name']),
            'U_VIEW_ITEM' => 'viewitem.php'.$SID.'&amp;' . URI_ITEM . '='.$item['item_id'],
            'VALUE' => $item['item_value'])
        );
    }
    
    $tpl->assign_vars(array(
        'L_MEMBERS_PRESENT_AT' => sprintf($user->lang['members_present_at'], stripslashes($raid['raid_name']), 
                                  date($user->style['date_notime_long'], $raid['raid_date'])),
        'L_ADDED_BY' => $user->lang['added_by'],
        'L_UPDATED_BY' => $user->lang['updated_by'],
        'L_NOTE' => $user->lang['note'],
        'L_VALUE' => $user->lang['value'],
        'L_DROPS' => $user->lang['drops'],
        'L_BUYER' => $user->lang['buyer'],
        'L_ITEM' => $user->lang['item'],
        'L_SPENT' => $user->lang['spent'],
        'L_ATTENDEES' => $user->lang['attendees'],
        
        'S_COLUMN0' => ( isset($s_column0) ) ? true : false,
        'S_COLUMN1' => ( isset($s_column1) ) ? true : false,
        'S_COLUMN2' => ( isset($s_column2) ) ? true : false,
        'S_COLUMN3' => ( isset($s_column3) ) ? true : false,
        'S_COLUMN4' => ( isset($s_column4) ) ? true : false,
        'S_COLUMN5' => ( isset($s_column5) ) ? true : false,
        'S_COLUMN6' => ( isset($s_column6) ) ? true : false,
        'S_COLUMN7' => ( isset($s_column7) ) ? true : false,
        'S_COLUMN8' => ( isset($s_column8) ) ? true : false,
        'S_COLUMN9' => ( isset($s_column9) ) ? true : false,
        
        'COLUMN_WIDTH' => ( isset($column_width) ) ? $column_width : 0,
        'COLSPAN' => $user->style['attendees_columns'],
        
        'RAID_ADDED_BY' => ( !empty($raid['raid_added_by']) ) ? stripslashes($raid['raid_added_by']) : 'N/A',
        'RAID_UPDATED_BY' => ( !empty($raid['raid_updated_by']) ) ? stripslashes($raid['raid_updated_by']) : 'N/A',
        'RAID_NOTE' => ( !empty($raid['raid_note']) ) ? stripslashes($raid['raid_note']) : '&nbsp;',
        'DKP_NAME' => $eqdkp->config['dkp_name'],
        'RAID_VALUE' => $raid['raid_value'],
        'ATTENDEES_FOOTCOUNT' => sprintf($user->lang['viewraid_attendees_footcount'], sizeof($attendees)),
        'ITEM_FOOTCOUNT' => sprintf($user->lang['viewraid_drops_footcount'], $db->num_rows($items_result)))
    );
    
    $page_title = sprintf($user->lang['title_prefix'], $eqdkp->config['guildtag'], $eqdkp->config['dkp_name']).': '.$user->lang['viewraid_title'];
    include_once($eqdkp_root_path . 'includes/page_header.php');
    
    $tpl->set_filenames(array(
        'body' => 'viewraid.html')
    );
    
    include_once($eqdkp_root_path . 'includes/page_tail.php');
}
else
{
    message_die($user->lang['error_invalid_raid_provided']);
}
?>